package com.youbug.mall.product_pms.service.impl;

import com.youbug.mall.product_pms.entity.PmsSkuInfo;
import com.youbug.mall.product_pms.entity.PmsSkuSaleAttrValue;
import com.youbug.mall.product_pms.mapper.PmsSkuSaleAttrValueMapper;
import com.youbug.mall.product_pms.service.IPmsSkuInfoService;
import com.youbug.mall.product_pms.service.IPmsSkuSaleAttrValueService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youbug.mall.product_pms.vo.AttrValueWithSkuIdVo;
import com.youbug.mall.product_pms.vo.SkuItemSaleAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import com.youbug.common.vo.QueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * sku销售属性&值 服务实现类
 * </p>
 *
 * @author youbug
 * @since 2024-04-23
 */
@Service
public class PmsSkuSaleAttrValueServiceImpl extends ServiceImpl<PmsSkuSaleAttrValueMapper, PmsSkuSaleAttrValue> implements IPmsSkuSaleAttrValueService {

        @Autowired
        private IPmsSkuInfoService skuInfoService;

        @Override
        public IPage<PmsSkuSaleAttrValue> getPmsSkuSaleAttrValuePage(QueryVo<PmsSkuSaleAttrValue> queryVo) {

        Page<PmsSkuSaleAttrValue> paramPage = new Page<>();

        paramPage.setCurrent(queryVo.getCurrentPage());

        paramPage.setSize(queryVo.getPageSize());

        QueryWrapper<PmsSkuSaleAttrValue> wrapper = new QueryWrapper<>();

        PmsSkuSaleAttrValue pmsSkuSaleAttrValue = queryVo.getPayload();

        if(!ObjectUtils.isEmpty(pmsSkuSaleAttrValue)){
            if(StringUtils.hasText(pmsSkuSaleAttrValue.getAttrName())){
                wrapper.eq("attr_name", pmsSkuSaleAttrValue.getAttrName());
            }
            if(StringUtils.hasText(pmsSkuSaleAttrValue.getAttrValue())){
                wrapper.eq("attr_value", pmsSkuSaleAttrValue.getAttrValue());
            }
        }
        if(!ObjectUtils.isEmpty(queryVo.getStartTime())){
            wrapper.gt("create_time", queryVo.getStartTime());
        }
        if(!ObjectUtils.isEmpty(queryVo.getEndTime())){
            wrapper.lt("create_time", queryVo.getEndTime());
        }
        wrapper.orderByDesc("create_time");

        Page<PmsSkuSaleAttrValue> page = this.page(paramPage, wrapper);

        return page;
        }


    @Transactional(transactionManager = "transactionManagerPms", rollbackFor = Exception.class)
    public boolean saveBatch(Collection<PmsSkuSaleAttrValue> entityList) {
        return saveBatch(entityList, DEFAULT_BATCH_SIZE);
    }

    @Override
    public List<SkuItemSaleAttrVo> getSaleAttrsBySpuId(Long spuId) {

        List<PmsSkuInfo> skus = skuInfoService.list(new QueryWrapper<PmsSkuInfo>().eq("spu_id", spuId));

        List<Long> skuIds = skus.stream().map(PmsSkuInfo::getSkuId).collect(Collectors.toList());

        List<PmsSkuSaleAttrValue> skuSaleAttrValues = this.list(new QueryWrapper<PmsSkuSaleAttrValue>().in("sku_id", skuIds));

        Map<String, List<PmsSkuSaleAttrValue>> listMap = skuSaleAttrValues.stream().collect(Collectors.groupingBy(PmsSkuSaleAttrValue::getAttrValue));

        Map<String, String> attrValueSkuIdsMap = new HashMap<>();

        listMap.forEach((k,v)->{
            attrValueSkuIdsMap.put(k, v.stream().map(e->e.getSkuId()+"").collect(Collectors.joining(",")));
        });

        Map<Long, SkuItemSaleAttrVo> skuSaleAttrMap = new HashMap<>();

        for (PmsSkuSaleAttrValue saleAttr : skuSaleAttrValues) {
            Long attrId = saleAttr.getAttrId();

            SkuItemSaleAttrVo attrVo = skuSaleAttrMap.get(attrId);

            if (attrVo != null) {

                List<AttrValueWithSkuIdVo> temp = attrVo.getAttrValues();
                boolean have = false;
                for (AttrValueWithSkuIdVo idVo : temp) {
                    if(saleAttr.getAttrValue().equals(idVo.getAttrValue())){
                        have = true;
                    }
                }
                if (have){continue;}
                attrVo.setAttrId(attrId);

                attrVo.setAttrName(saleAttr.getAttrName());

                temp.add(new AttrValueWithSkuIdVo(saleAttr.getAttrValue(), null));

                attrVo.setAttrValues(temp);
            } else {
                List<AttrValueWithSkuIdVo> temp = new ArrayList<>();

                temp.add(new AttrValueWithSkuIdVo(saleAttr.getAttrValue(), null));

                attrVo = new SkuItemSaleAttrVo();

                attrVo.setAttrValues(temp);

                attrVo.setAttrId(attrId);

                attrVo.setAttrName(saleAttr.getAttrName());

                skuSaleAttrMap.put(attrId, attrVo);
            }
        }
        List<SkuItemSaleAttrVo> collect = new ArrayList<>(skuSaleAttrMap.values());

        collect.forEach(e->{
            e.getAttrValues().forEach(v->{
                String skuIdsTemp = attrValueSkuIdsMap.get(v.getAttrValue());
                v.setSkuIds(skuIdsTemp);
            });
        });
        return collect;
    }
}
